package com.game.view.component.dropItem
{
	import base.BaseHero;
	import base.BaseObject;
	
	import com.dusk.util.ArrayUtil;
	import com.game.data.constant.FrameRate;
	import com.greensock.TweenMax;
	
	import flash.geom.Rectangle;
	
	public class DropItemBase extends BaseObject
	{
		public function DropItemBase()
		{
			super();
			existTime = 30 * FrameRate.ANIMATE_FRAME_RATE;
		}
		
		public var funcWhenPick:Array = [];
		protected var existTime:uint = 0;
		protected var listening:Boolean = true;
		
		override protected function initCollideArea():void
		{
			collideBox = new Rectangle(-25, -50, 50, 50);
			super.initCollideArea();
		}
		
		override public function render(ratio:Number = 1):void
		{
			if(!listening)
			{
				return;
			}
			super.render(ratio);
			for each(var bh:BaseHero in gc.team.getHeroArray())
			{
				if (bh.isDead() || Math.abs(x - bh.x) > 300 || Math.abs(y - bh.y) > 200)
				{
					continue;
				}
				checkHit(bh);
			}
		}
		
		override public function renderAnimate():void
		{
			super.renderAnimate();
			if(isNaN(existTime)) return;
			if (--existTime <= 0)
			{
				destroy();
			}
		}
		
		public function addPickFunc(func:Function):void
		{
			if (!ArrayUtil.contains(funcWhenPick, func))
			{
				funcWhenPick.push(func);
			}
		}
		
		public function removePickFunc(func:Function):void
		{
			ArrayUtil.removeElement(funcWhenPick, func);
		}
		
		public function setExistTime(frame:int):void
		{
			existTime = frame;
		}
		
		protected function checkHit(bh:BaseHero):void
		{
			if(checkCollideTarget(bh))
			{
				listening = false;
				if (funcWhenPick.length)
				{
					for each(var func:Function in funcWhenPick)
					{
						func(bh);
					}
				}
				TweenMax.to(this, 0.8, {
					"y"         : y - 100,
					"alpha"     : 0,
					"onComplete": destroy
				});
			}
		}
		
		override public function destroy():void
		{
			removeFromParent();
			ArrayUtil.removeAllElement(funcWhenPick);
			isReadyToDestroy = true;
		}
	}
}
